﻿namespace AB.ServiceDirectory.Dao
{
    using System;
    using System.Linq;
    using Entities;
    using Commons.DataFilters;

    /// <summary>
    /// GovOfficeRegionDao class
    /// </summary>
    [Serializable]
    public class GovOfficeRegionDao
    {
        /// <summary>
        /// Data Context
        /// </summary>
        [NonSerialized]
        private readonly ServicesDirectoryDataContext _context = DB.Context;

        /// <summary>
        /// Gets the gov office regions.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <param name="startRowIndex">Start index of the row.</param>
        /// <param name="max">The max.</param>
        /// <returns>
        /// GovOfficeRegion list
        /// </returns>
        public IQueryable<GovOfficeRegion> GetList(GovOfficeRegionFilter filter, string sortType, int startRowIndex, int max)
        {
            var data = from gor in _context.GovOfficeRegions
                       select gor;

            // 1. Filter by condition
            if (filter != null)
            {
                data = GetListQuery(data, filter);
            }

            // 2. Sorting
            data = GetListSort(data, sortType);

            // 3. Limit the rows
            data = data.Skip(startRowIndex).Take(max);

            return data;
        }

        /// <summary>
        /// Gets the list sort.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <returns></returns>
        private static IQueryable<GovOfficeRegion> GetListSort(IQueryable<GovOfficeRegion> data, string sortType)
        {
            var sortDescending = false;
            if (!string.IsNullOrEmpty(sortType))
            {
                var values = sortType.Split(' ');
                sortType = values[0];

                if (values.Length > 1)
                {
                    sortDescending = (values[1] == "DESC");
                }

                switch (sortType)
                {
                    case "GovOfficeRegionName":
                        data = sortDescending
                                   ? data.OrderByDescending(g => g.GovOfficeRegionName)
                                   : data.OrderBy(g => g.GovOfficeRegionName);
                        break;
                    case "Description":
                        data = sortDescending
                                   ? data.OrderByDescending(g => g.Description)
                                   : data.OrderBy(g => g.Description);
                        break;
                    case "IsActive":
                        data = sortDescending
                                   ? data.OrderByDescending(g => g.IsActive)
                                   : data.OrderBy(g => g.IsActive);
                        break;
                    default:
                        data = sortDescending
                                   ? data.OrderByDescending(g => g.GovOfficeRegionId)
                                   : data.OrderBy(g => g.GovOfficeRegionId);
                        break;
                }
            }

            return data;
        }

        /// <summary>
        /// Gets the list query.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        private static IQueryable<GovOfficeRegion> GetListQuery(IQueryable<GovOfficeRegion> data, GovOfficeRegionFilter filter)
        {
            // Filter by beginning of the letters
            if (filter.BeginningLetters != null)
            {
                data = data.Where(g => filter.BeginningLetters.Contains(g.GovOfficeRegionName[0].ToString()));
            }

            // Filter by in-active & active Gor
            if (!filter.IncludeInactive)
            {
                data = data.Where(g => g.IsActive == !filter.IncludeInactive);
            }

            return data;
        }

        /// <summary>
        /// Gets the list count.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public int GetListCount(GovOfficeRegionFilter filter)
        {
            var data = from gor in _context.GovOfficeRegions
                       select gor;

            return GetListQuery(data, filter).Count();
        }

        /// <summary>
        /// Gets the gov office region by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>GovOfficeRegion object</returns>
        public GovOfficeRegion GetGovOfficeRegionById(int id)
        {
            var govOfficeRegion = _context.GovOfficeRegions.FirstOrDefault(g => g.GovOfficeRegionId == id);
            return govOfficeRegion;
        }
    }
}
